package com.example.tests;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Random;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.DataProvider;
import com.example.fw.ApplicationManager;

import static com.example.tests.GroupDataGenerator.generateRandomGroups;;

public class BaseClass {

	public static ApplicationManager app;

	// private String name;

	@BeforeTest
	public void setUp() throws Exception {
		app = new ApplicationManager();
		app.start();
	}

	@AfterTest
	public void tearDown() throws Exception {
		app.stop();
	}

	@DataProvider
	public Iterator<Object[]> randomValidGroupGenerator() {
		return wrapGroupForDataProvider(generateRandomGroups(5)).iterator();
	}

	private List<Object[]> wrapGroupForDataProvider(List<GroupData> groups) {
		List<Object[]> list = new ArrayList<Object[]>();
		for (GroupData group : groups) {
			list.add(new Object[]{group});
		}
		return list;
	}

//	@DataProvider
//	public Iterator<Object[]> randomValidContactsGenerator() {
//		List<Object[]> list = new ArrayList<Object[]>();
//		for (int i = 0; i < 5; i++) {
//			ContactData grcontact = new ContactData()
//					.withFirstname(generateRandomString())
//					.withLastname(generateRandomString())
//					.withAddress1(generateRandomString())
//					.withHome(generateRandomNumber())
//					.withMobile(generateRandomNumber())
//					.withWork(generateRandomNumber())
//					.withEmail(generateRandomString())
//					.withEmail2(generateRandomString())
//					.withBday(generateRandomDay())
//					.withBmonth(generateRandomMonth())
//					.withByear(generateRandomYear())
//					.withGroup(generateRandomGroup())
//					.withAddress2(generateRandomString())
//					.withPhone2(generateRandomNumber());
//			list.add(new Object[] { grcontact });
//
//		}
//		return list.iterator();
//	}

	public String generateRandomNumber() {
		Random rnd_num = new Random();
		if (rnd_num.nextInt(3) == 0) {
			return "";
		} else {
			return "" + rnd_num.nextInt(9999999);
		}
	}

	public String generateRandomGroup() {
		Random random = new Random();
		List<GroupData> group_list = app.getGroupHelper().getGroups();
		int index = random.nextInt(group_list.size());

		GroupData groupData = group_list.get(index);
		String name = groupData.getName();

		return name;

	}

	public String generateRandomMonth() { // Date Calendar
		String names[] = { "January", "February", "March", "April", "May",
				"June", "July", "August", "September", "October", "November",
				"December" };
		Random rnd_month = new Random();
		if (rnd_month.nextInt(3) == 0) {
			return "-";
		} else {
			int n = rnd_month.nextInt(12);
			return names[n];
		}
	}

	public String generateRandomDay() {
		Random rnd_num = new Random();
		if (rnd_num.nextInt(3) == 0) {
			String day = "-";
			return day;
		} else {
			int intday = rnd_num.nextInt(30) + 1;
			String day = Integer.toString(intday);
			return day;
		}
	}

	public String generateRandomYear() {
		Random rnd_num = new Random();
		if (rnd_num.nextInt(3) == 0) {
			String year = "-";
			return year;
		} else {
			int intyear = rnd_num.nextInt(114) + 1900;
			String year = Integer.toString(intyear);
			return year;
		}
	}
}
